import { observer, useLocalObservable } from 'mobx-react'
import Store from './store'
import { SocketContainer } from '@/hooks/useSocket'
import { getCookie, getLocalStorage } from '@/storage'
import { kingWs } from '@/servers/kingUrl'

export default observer((props: any) => {
    const store = useLocalObservable(() => Store)
    const userData = getLocalStorage('USER_DATA')

    return (
        userData?.uid && (
            <SocketContainer.Provider
                initialState={{
                    url: kingWs,
                    time: 10000,
                    params: {
                        channel: 'introduction',
                        token: getCookie('TOKEN'),
                        user_id: userData?.uid,
                        role: 'client',
                    },
                    init: ({ send, onMessage, close }) => {
                        onMessage('close', () => {
                            close()
                        })

                        /** 建立连接 */
                        send('connect')
                    },
                    noReconnect: true,
                }}
            >
                {props.children}
            </SocketContainer.Provider>
        )
    )
})
